import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:url_launcher/url_launcher.dart';

import '../../common.dart';

class Order extends StatefulWidget {
  Order({super.key});
  @override
  State<Order> createState() => _QGRulePageState();
}

class _QGRulePageState extends State<Order> {
  bool tindex = true;

  final uuid = Get.parameters['uuid'];
  Map info = {};
  Map materials = {};
  List detailimages = [];
  Map makeskus = {};
  Map addressInfo = {};

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getData(url: '/third/$uuid').then((res) {
      info = res;
      materials = res['materials'];
      detailimages = res['detail_images'];
      makeskus = res['make_skus'][0];
      addefault();
    });
  }

  addefault() {
    getData(url: '/addresses_default').then((res) {
      if (res['addresses'].isNotEmpty) {
        addressInfo = res['addresses'][0];
      }
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Color(0XFFFFFFFF),
        centerTitle: true,
        title: wText(text: '', size: 18, bold: true),
        leading: buildBackButton(context),
      ),
      backgroundColor: Color(0xFFFFFFFF),
      body: Column(
        children: [
          Expanded(
            child: ListView(
              padding: EdgeInsets.symmetric(horizontal: 15),
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    Container(
                      width: 130,
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          wText(text: '消耗藏品', color: '878b95'),
                          SizedBox(height: 5),
                          wImage(
                            url: '${materials['thumb']}',
                            width: 130,
                            height: 130,
                            borderRadius: 10,
                          ),
                          SizedBox(height: 5),

                          wText(text: '${materials['title']}'),
                        ],
                      ),
                    ),
                    wImage(url: 'youbian', width: 35),
                    Container(
                      width: 130,
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          wText(text: '兑换商品', color: '878b95'),
                          SizedBox(height: 5),
                          wImage(
                            url: '${makeskus['thumb']}',
                            width: 130,
                            height: 130,
                            borderRadius: 10,
                          ),
                          SizedBox(height: 5),

                          wText(text: '${makeskus['title']}'),
                        ],
                      ),
                    ),
                  ],
                ),
                if (addressInfo.isNotEmpty)
                  GestureDetector(
                    onTap: () {
                      toNamed('/web_tabs/shopping/mall_address').then((res) {
                        addefault();
                      });
                    },
                    child: Container(
                      margin: EdgeInsets.only(top: 30),
                      padding: EdgeInsets.symmetric(
                        horizontal: 12,
                        vertical: 12,
                      ),
                      decoration: BoxDecoration(
                        color: Color(0XFFF8F9FD),
                        borderRadius: BorderRadius.circular(10),
                      ),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Container(
                            width: 250,
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                wText(
                                  text:
                                      '${addressInfo['consignee']} ${addressInfo['phone']}',
                                  size: 15,
                                  bold: true,
                                ),
                                wText(
                                  text:
                                      '${addressInfo['province']} ${addressInfo['city']} ${addressInfo['district']} ${addressInfo['address']}',
                                ),
                              ],
                            ),
                          ),
                          wText(text: '修改', size: 15, color: '6828FF'),
                        ],
                      ),
                    ),
                  ),
                if (addressInfo.isEmpty)
                  GestureDetector(
                    onTap: () {
                      toNamed('/web_tabs/shopping/mall_address').then((res) {
                        addefault();
                      });
                    },
                    child: Container(
                      margin: EdgeInsets.only(top: 30),
                      padding: EdgeInsets.symmetric(
                        horizontal: 12,
                        vertical: 12,
                      ),
                      decoration: BoxDecoration(
                        color: Color(0XFFF8F9FD),
                        borderRadius: BorderRadius.circular(10),
                      ),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          wImage(url: 'dizhi', width: 17),
                          SizedBox(width: 5),
                          wText(text: '请添加收货地址', size: 15),
                          Expanded(child: SizedBox()),
                          wImage(url: 'hbottom', width: 12),
                        ],
                      ),
                    ),
                  ),
                Container(
                  margin: EdgeInsets.only(top: 10),
                  padding: EdgeInsets.symmetric(horizontal: 12, vertical: 12),
                  decoration: BoxDecoration(
                    color: Color(0XFFF8F9FD),
                    borderRadius: BorderRadius.circular(10),
                  ),
                  child: Column(
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          wText(text: '商品类型', size: 14, color: '878b95'),
                          wText(text: '实物商品', size: 14),
                        ],
                      ),
                      SizedBox(height: 20),

                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          wText(text: '兑换数量', size: 14, color: '878b95'),
                          wText(text: '1', size: 14),
                        ],
                      ),
                      SizedBox(height: 20),

                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          wText(text: '兑换类型', size: 14, color: '878b95'),
                          wText(text: '普通兑换', size: 14),
                        ],
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
          SizedBox(height: 15),
          GestureDetector(
            onTap: () {
              toNamed('/agreement?type=1');
            },
            child: Column(
              children: [
                Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    GestureDetector(
                      onTap: () {
                        tindex = !tindex;
                        setState(() {});
                      },
                      child: wImage(
                        url: tindex ? 'yuans' : 'yuan',
                        width: 13,
                        height: 13,
                      ),
                    ),
                    SizedBox(width: 5),
                    wText(text: '我已阅读并同意《服务协议》'),
                  ],
                ),
                SizedBox(height: 10),

                GestureDetector(
                  onTap: () {
                    if (addressInfo.isNotEmpty) {
                      wTan(
                        text: Column(
                          children: [
                            Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                wText(text: '兑换后，指定藏品', size: 18),
                                wText(text: '将销毁', size: 18, color: '6828FF'),
                              ],
                            ),
                            wText(text: '此操作不可逆', size: 18),
                          ],
                        ),
                        click: () async {
                          postData(
                            url: '/third/$uuid/confirm',
                            data: {'num': '1'},
                          ).then((res) {
                            if (res != null) {
                              back();
                              toNamed('/home/exchange/suu');
                            }
                          });
                        },
                      );
                    } else {
                      showTost('添加地址');
                    }
                  },
                  child: Container(
                    padding: EdgeInsets.symmetric(horizontal: 25),
                    margin: EdgeInsets.symmetric(horizontal: 15),
                    height: 50,
                    decoration: BoxDecoration(
                      color: Color(0XFF0F172B),
                      borderRadius: BorderRadius.circular(50),
                    ),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        wText(text: '立即兑换', color: 'FFFFFF', size: 18),
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
          SizedBox(height: 15),
        ],
      ),
    );
  }
}
